//
//  LZMyCouponsTableView.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/31.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyCouponsTableView.h"
#import "LZMyCouponsTableViewCell.h"
#import "LZMyGroupBuyTableViewCell.h"
#import "LZMyCouponsModel.h"
#import "LZMyRestaurantCoupons.h"
#import "LZMyCouponsTableViewHeaderView.h"

#import "Masonry.h"

@interface LZMyCouponsTableView() <UITableViewDelegate, UITableViewDataSource>

@end

@implementation LZMyCouponsTableView


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self setup];
    }
    return self;
}

#pragma
#pragma mark private method

- (void)setup {
    self.backgroundColor = UIColorFromRGB(0xffffff, 1);
    self.separatorColor = [UIColor clearColor];
    self.dataSource = self;
    self.delegate = self;
    [self registerClass:[LZMyCouponsTableViewHeaderView class] forHeaderFooterViewReuseIdentifier:@"restauranttitle"];
}

#pragma
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArrays.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    LZMyRestaurantCoupons * restaurantModel = self.dataArrays[section];
    if (restaurantModel) {
        return restaurantModel.couponDTO.count;
    }
    else
        return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.viewType == 1 || self.viewType == 2 || self.viewType == 3) {
        LZMyCouponsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"couponsCellIdentifier"];
        if (!cell) {
            cell = [[LZMyCouponsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"couponsCellIdentifier"];
        }
        
        LZMyRestaurantCoupons * restModel = self.dataArrays[indexPath.section];
        if (restModel) {
            LZMyCouponsModel * coupModel = restModel.couponDTO[indexPath.row];
            if (coupModel) {
                cell.viewType = self.viewType;
                cell.model = coupModel;
            }
        }
        
        return cell;
    }
    else {
        LZMyGroupBuyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupBuyCellIdentifier"];
        if (!cell) {
            cell = [[LZMyGroupBuyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"groupBuyCellIdentifier"];
        }
        
        if (indexPath.row == 0) {
            cell.cellType = LZ_MYGOUPONSBUY_CELL_TYPE_QUIT;
        }
        else {
            cell.cellType = LZ_MYGOUPONSBUY_CELL_TYPE_CANCEL;
        }
        
        LZMyRestaurantCoupons * restModel = self.dataArrays[indexPath.section];
        if (restModel) {
            LZMyCouponsModel * coupModel = restModel.couponDTO[indexPath.row];
            if (coupModel) {
                
                if (coupModel.isCurrentUser) {
                    cell.cellType = LZ_MYGOUPONSBUY_CELL_TYPE_CANCEL;
                } else {
                    cell.cellType = LZ_MYGOUPONSBUY_CELL_TYPE_QUIT;
                }
                cell.model = coupModel;
            }
        }
        
        return cell;
    }
}

#pragma
#pragma mark UITableViewDelegate

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    LZMyRestaurantCoupons * restModel = self.dataArrays[section];
    LZMyCouponsTableViewHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"restauranttitle"];
    [headerView setText:restModel.restaurantName];
    
    return headerView;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView * v = [[UIView alloc] init];
    v.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    LZMyRestaurantCoupons * restModel = self.dataArrays[section];
    
    LZMyCouponsTableViewHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"restauranttitle"];
    [headerView setText:restModel.restaurantName];
    
    return headerView.intrinsicContentSize.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (self.viewType == 1 || self.viewType == 2 || self.viewType == 3) {
        return 10.0f;
    }
    else {
        return 0.001f;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.viewType == 1 || self.viewType == 2 || self.viewType == 3) {
        return 96.0f;
    }
    else {
        return 180.0;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(_didSelectRowBlock) {
        _didSelectRowBlock(indexPath);
    }
}

@end
